﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using 凌依.工具库;

public static class 扩展方法 
{
    public static float 距离(this Vector3 起始 ,Vector3 结束)
    {
        return Vector3.Distance(起始, 结束);
    }

    public static Vector3 ToVector3(this Vector2 默认)
    {
        return new Vector3(默认.x, 默认.y, 0);
    }
    public static Vector2Int ToVector2Int(this Vector2 默认)
    {
        return new Vector2Int((int)默认.x, (int)默认.y);
    }
    public static Vector3Int ToVector3Int(this Vector2 默认)
    {
        return new Vector3Int((int)默认.x, (int)默认.y,0);
    }
    public static Vector2 ToVector2(this Vector3 默认)
    {
        return new Vector2(默认.x, 默认.y);
    }
    public static Vector3Int ToVector3Int(this Vector2Int 默认)
    {
        return new Vector3Int(默认.x, 默认.y,0);
    }

    public static Vector2Int[] Get4pos(this Vector2Int 默认)
    {
        Vector2Int[] 返回 = new Vector2Int[4]
        {
            new Vector2Int(默认.x,默认.y+1),
            new Vector2Int(默认.x,默认.y-1),
            new Vector2Int(默认.x+1,默认.y),
            new Vector2Int(默认.x-1,默认.y)
        };
        return 返回;
    }

    /// <summary>
    /// 格式: "1,2" = "x,y"
    /// </summary>
    /// <param name="本体"></param>
    /// <returns></returns>
    public static Vector2Int ToVector2Int(this string 本体)
    {
        string[] vs = 本体.Split(',');
        return new Vector2Int(int.Parse(vs[0]), int.Parse(vs[1]));
    }

    public static string ToStringVe2(this Vector2Int 本体)=> $"{本体.x},{本体.y}";

    public static Vector3 屏幕坐标(this Vector3 默认)
    {
        return Camera.main.WorldToScreenPoint(默认);
    }

    public static Vector3 世界坐标(this Vector3 默认)
    {
        return Camera.main.ScreenToWorldPoint(默认);
    }

    public static Vector3 改Z(this Vector3 默认,float z)
    {
        return new Vector3(默认.x, 默认.y, 默认.z + z);
    }
    public static Vector3 改Y(this Vector3 默认, float Y)
    {
        return new Vector3(默认.x, 默认.y+Y, 默认.z);
    }
    public static Vector3 改X(this Vector3 默认, float X)
    {
        return new Vector3(默认.x+X, 默认.y, 默认.z);
    }
    
    public static int ToInt(this string 自己)=> int.Parse(自己);
    public static float ToFloat(this string 自己)=> float.Parse(自己);

    public static Vector2 ToVector2(this string 自己)
    {
        string 设置 = 自己;
        if (自己.Contains("|"))
            设置 = 设置.Replace('|', ',');

        string[] vs = 设置.Split(',');
        if (vs.Length <= 1)
            return Vector2.zero;

        return new Vector2(vs[0].ToFloat(), vs[1].ToFloat());
    }

    public static Color ToColor(this string color)
    {
        if (color[0] != '#')
            return new Color();

        if (color.Length == 7)
        {
            int r = 进制转换.十六转十(""+color[1] + color[2]);
            int g = 进制转换.十六转十("" + color[3] + color[4]);
            int b = 进制转换.十六转十("" + color[5] + color[6]);

            float rf = r / 255f;
            float gf = g / 255f;
            float bf = b / 255f;

            return new Color(rf, gf, bf);
        }
        else if(color.Length == 9)
        {
            int r = 进制转换.十六转十("" + color[1] + color[2]);
            int g = 进制转换.十六转十("" + color[3] + color[4]);
            int b = 进制转换.十六转十("" + color[5] + color[6]);
            int a = 进制转换.十六转十("" + color[7] + color[8]);

            float rf = r / 255f;
            float gf = g / 255f;
            float bf = b / 255f;
            float af = a / 255f;

            return new Color(rf, gf, bf, af);
        }
        else
            return new Color();
    }
}
